<template>
  <div class="mod-config">
    <basic-container>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="活动名称1" prop="bannerName">
          <el-input
            v-model.trim="queryParams.bannerName"
            placeholder="请输入活动名称"
            clearable
            size="small"
            @keyup.enter.native="getDataList"
          />
        </el-form-item>

        <el-form-item label="上架状态:" prop="status">
          <el-select
            v-model="queryParams.status"
            filterable
            placeholder="请选择状态"
            clearable
            size="small"
            class="margin-left-xs"
          >
            <el-option
              v-for="dict in statusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            v-if="permissions.order_comment_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
        </el-form-item>

        <el-form-item>
          <el-button
            v-if="permissions.order_comment_export"
            icon="el-icon-download"
            type="primary"
            @click="handleExportTemp()"
            >导出空模板</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="permissions.order_comment_export"
            icon="el-icon-upload2"
            type="primary"
            @click="$refs.excelUserUpload.show()"
            >导入数据</el-button
          >
        </el-form-item>

      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column
            type="index"
            header-align="center"
            :index="indexMethod"
            align="center"
            label="编号"
          >
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="用户"
            width="140"
          >
            <template slot-scope="scope">
              <div @click="handleModefyAvatar(scope.row)">
                <el-avatar icon="el-icon-user-solid" :src="scope.row.headerUrl"></el-avatar>
              </div>
              <div>
                <span>{{scope.row.nickName}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="spuTitle"
            header-align="center"
            align="center"
            label="商品信息"
          >
          </el-table-column>
          
          <el-table-column
            prop="sort"
            header-align="center"
            align="left"
            label="订单信息"
          >
            <template slot-scope="scope">
              <div>
                <span>订单号：</span>
                <span>{{scope.row.orderId ? scope.row.orderId : '-'}}</span>
              </div>
              <div>
                <span>购买价格：</span>
                <span>{{scope.row.sellPrice ? '￥' + formatMoneyToYuan(scope.row.sellPrice) : '-'}}</span>
              </div>
              <div>
                <span>购买数量：</span>
                <span>{{scope.row.buyCount ? scope.row.buyCount : '-'}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="评价来源"
            width="120">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.commentSource === '1'">买家评论</el-tag>
              <el-tag type="info" v-else>平台导入</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="left"
            label="首评"
            width="240px">
            <template slot-scope="scope">
              <div>
                <span>评论时间:</span>
                <span>{{scope.row.createTime}}</span>
              </div>
              <div style="display: flex;">
                <div>商品评分:</div>
                <div style="margin:2px 0px 0px 10px"><el-rate v-model="scope.row.goodsScore" disabled></el-rate></div>
              </div>
              <div style="display: flex;">
                <div>服务评分:</div>
                <div style="margin:2px 0px 0px 10px"><el-rate v-model="scope.row.serverScore" disabled></el-rate></div>
              </div>
              <div style="display: flex;">
                <div>物流评分:</div>
                <div style="margin:2px 0px 0px 10px"><el-rate v-model="scope.row.deliveryScore" disabled></el-rate></div>
              </div>

              <div v-if="scope.row.userFirstComment">
                <div>
                  <i class="el-icon-chat-dot-round" style="color:#FF0000;"></i>
                  <span style="color:#FF0000; margin-left:4px">买家留言：</span>
                  <span>{{scope.row.userFirstComment.createTime}}</span>
                </div>
                <div>
                  <span>{{scope.row.userFirstComment.content}}</span>
                </div>
              </div>

              <div v-if="scope.row.platformFirstComment" style="margin-top:10px">
                <div>
                  <i class="el-icon-chat-dot-round" style="color:#FF0000;"></i>
                  <span style="color:#FF0000; margin-left:4px">卖家回复：</span>
                  <span>{{scope.row.platformFirstComment.createTime}}</span>
                </div>
                <div>
                  <span>{{scope.row.platformFirstComment.content}}</span>
                </div>
                <el-button
                  v-if="permissions.order_comment_edit"
                  type="text"
                  size="small"
                  @click="handleUpdatePlatformReply(scope.row)"
                  >修改卖家回复</el-button
                >
              </div>
              <div v-else >
                <el-button
                  v-if="permissions.order_comment_add"
                  type="text"
                  size="small"
                  @click="handlePlatformReply(scope.row)"
                  >立即回复</el-button
                >
              </div>

            </template>
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                v-if="permissions.order_comment_edit"
                type="text"
                size="small"
                icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.id)"
                >修改买家评论</el-button
              >
              <el-button
                v-if="permissions.order_comment_del"
                type="text"
                size="small"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="queryParams.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <EditForm
        v-if="visible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :visible.sync="visible"
      ></EditForm>
    </basic-container>

    <excel-upload
        ref="excelUserUpload"
        title="用户信息导入"
        url="/shop_user/user/importUser"
        @refreshDataList="getDataList"
    ></excel-upload>

  </div>
</template>

<script>
import { fetchList, delObj, platformReply, updatePlatformReply } from "@/api/shop_order/order_comment";
import EditForm from "./edit";
import { mapGetters } from "vuex";
import ExcelUpload from '@/components/upload/excel'

export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        bannerName:'',
        status:''
      },
      dataList: [],

      statusList:[{
        value:'1',
        label:'上架'
      },{
        value:'0',
        label:'下架'
      }],

      totalPage: 0,
      dataListLoading: false,
      visible: false,
    };
  },
  components: {
    EditForm,
    ExcelUpload
  },
  created() {
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    /**
     * 获取数据列表
     */
    getDataList() {
      this.dataListLoading = true;
      fetchList(
        Object.assign({
          bannerName: this.queryParams.bannerName,
          status: this.queryParams.status,
          current: this.queryParams.pageIndex,
          size: this.queryParams.pageSize
        })
      ).then((response) => {
        this.dataListLoading = false;
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
      });
    },

    /**
     * 每页数
     */
    sizeChangeHandle(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageIndex = 1;
      this.getDataList();
    },

    /**
     * 当前页
     */
    currentChangeHandle(val) {
      this.queryParams.pageIndex = val;
      this.getDataList();
    },

    /**
     * 新增 / 修改
     */
    addOrUpdateHandle(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },

    /**
     * 删除
     */
    deleteHandle(id) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          if(this.dataList.length <= 1 && this.queryParams.pageIndex > 1){
            this.queryParams.pageIndex--;
          }
          this.getDataList();
        })
        .catch(() => {});
    },

    /**
     * 平台回复
     */
    handlePlatformReply(row){
      this.$prompt("卖家回复", "回复", {
        closeOnClickModal: false,
        closeOnPressEscape: false,
        inputType: "textarea",
        inputPattern: /.+$/,
        inputErrorMessage: "回复内容不能为空",
        beforeClose: (action, { inputValue }, done) => {
          if (action !== "confirm") {
            return done();
          }
          const loading = this.$loading({ lock: true });
          let data = {
            orderCommentId: row.id,
            content: inputValue
          }
          platformReply(data)
            .then(() => {
              this.getDataList()
              this.$message.success("商家回复成功");
              done();
            })
            .finally(() => {
              loading.close();
            });
        }
      });
    },

    /**
     * 修改平台回复
     */
    handleUpdatePlatformReply(row){
      this.$prompt("修改卖家回复", "修改回复", {
        closeOnClickModal: false,
        closeOnPressEscape: false,
        inputType: "textarea",
        inputPattern: /.+$/,
        inputErrorMessage: "回复内容不能为空",
        inputValue:row.platformFirstComment.content,
        beforeClose: (action, { inputValue }, done) => {
          if (action !== "confirm") {
            return done();
          }
          const loading = this.$loading({ lock: true });
          let data = {
            orderCommentId: row.id,
            content: inputValue
          }
          updatePlatformReply(data)
            .then(() => {
              this.getDataList()
              this.$message.success("修改成功");
              done();
            })
            .finally(() => {
              loading.close();
            });
        }
      });
    },

    /**
     * 重置
     */
    resetQuery() {
      this.queryParams.pageIndex = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.getDataList();
    },

    /**
     * 修改头像
     */
    handleModefyAvatar(item){
      console.log(item)
    },

    indexMethod(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
  },
};
</script>
